// pages/detail/detail.js
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    commentShow: true,
    focus: true,
    requestUrl: 'detail',
    nodes: [{
      name: 'div',
      attrs: {
        class: 'div_class',
        style: 'line-height: 60px; color: red;'
      },
      children: [{
        type: 'text',
        text: 'Hello&nbsp;World!'
      }]
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.id = options.id;
    this.setData({
      'commentShow': this.data.commentShow,
      picUrl: app.globalData.picUrl
    });

    this.getDetailById();
  },

  getDetailById: function(){
    var that = this;
    wx.request({
      url: app.globalData.requestUrl + 'detail',
      method: 'POST',
      data: {id: this.data.id},
      header: {
        'ConTent-Type': 'application/x-www-form-urlencoded'
      },
      success: function(date){
        if(date.data.code == 200){
          console.log(date)
          var content = date.data.data.detail.content;console.log(content)
          content = content.replace(/<img/gi, '<img style="max-width:100%;height:auto;display:block" ').replace(/<section/g, '<div').replace(/\/section>/g, '\div>').replace(/src="/g, 'src="'+app.globalData.picUrl).replace(/<p/gi, '<p style="font-size: 12px; font-weight: normal; line-height: 22px; color: #3e3e3e; " ');
          that.setData({
            detail: date.data.data.detail,
            content: content,
            time: date.data.data.detail.create_date.substring(0,10)
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onShowCommentInput: function(event){console.log(3333)
    this.setData({
      'commentShow': !this.data.commentShow
    })
  },

  onSendComment: function(event){
      this.setData({
        'commentShow': !this.data.commentShow
      })
      this.data.focus = false;
  },

  ontest: function(){
    var that = this;
      setTimeout(function(){
        if(that.data.focus){
          that.setData({
            'commentShow': !that.data.commentShow
          })
        }
        that.data.focus = true;
      },500);
      
  },

  onShowComment: function(event){
    wx.navigateTo({
      url: '../../comment/conmment',
    })
  }
})